<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Exception;

class Cloudaccount
{
    private $app_dealer_id = '';
    private $app_broker_id = '';
    private $app_key = '';
    private $app_des3_key = '';
    private $app_private_key = '';
    private $yzh_public_key = '';

    public function __construct(){
        //获取云账户配置
        $config_arr = require __DIR__."/../../../adminconfig/yzhconfig.php";
        $this->app_dealer_id = $config_arr['app_dealer_id'];
        $this->app_broker_id = $config_arr['app_broker_id'];
        $this->app_key = $config_arr['app_key'];
        $this->app_des3_key = $config_arr['app_des3_key'];
        $this->app_private_key = $config_arr['app_private_key'];
        $this->yzh_public_key = $config_arr['yzh_public_key'];
    }


    /**
     * 获取云账户协议
     * User:@hang
     */
    public function getCloudaccountAgreement(){
        //配置基础信息
        $config = \Yzh\Config::newFromArray(array(
            "app_dealer_id" => $this->app_dealer_id,
            "app_broker_id" => $this->app_broker_id,
            "app_key" => $this->app_key,
            "app_des3_key" => $this->app_des3_key,
            "app_private_key" => $this->app_private_key,
            "yzh_public_key" => $this->yzh_public_key,
            "sign_type" => "rsa"
        ));
        try {
            $client = new \Yzh\ApiUserSignServiceClient($config);
            $client->setEnv(\Yzh\ApiUserSignServiceClient::ENV_PROD);
        } catch (\Exception $e) {
            die($e->getMessage());
        }
        // 配置请求参数
        $request = new \Yzh\Model\Apiusersign\ApiUserSignContractRequest(array(
            "dealer_id" => $this->app_dealer_id,
            "broker_id" => $this->app_broker_id,
        ));
        // request-id：请求ID，请求的唯一标识
        // 建议平台企业自定义 request-id，并记录在日志中，便于问题发现及排查
        // 如未自定义 request-id，将使用 SDK 中的 random 方法自动生成。注意：random 方法生成的 request-id 不能保证全局唯一，推荐自定义 request-id
        $request->setRequestID(time().rand(11,99).rand(100,999));
        try {
            $response = $client->apiUserSignContract($request);
            if ($response->isSuccess()) {
                // 操作成功
                $arr = array();
                $arr['title'] = $response->getData()->getTitle();
                $arr['url'] = $response->getData()->getUrl();
                return api_result('200','获取云账户协议信息成功',$arr);
            } else {
                // 失败返回
                return api_result('2001','获取协议地址失败code：'.$response->getCode().$response->getMessage(),[]);
            }
        } catch(Exception $e) {
            // 发生异常
            return api_result('2001','异常：'.$e->getMessage(),[]);
        }
    }


    /**
     * 执行用户签约云账户
     * User:@hang
     */
    public function setSignCloudaccount($member_id,$personal_name,$personal_idcard){
//        //开发阶段 直接通过
//        Db::table('fa_member')->where('member_id', $member_id)->update(array('me_yzh'=>1));
//        return ['code'=>'200','msg'=>'云账户签约成功','data'=>array()];


        if(empty($personal_name) || empty($personal_idcard)){
            return ['code'=>'500','msg'=>'请您先进行实名认证','data'=>array()];
        }
        // 配置基础信息
        $config = \Yzh\Config::newFromArray(array(
            "app_dealer_id" => $this->app_dealer_id,
            "app_broker_id" => $this->app_broker_id,
            "app_key" => $this->app_key,
            "app_des3_key" => $this->app_des3_key,
            "app_private_key" => $this->app_private_key,
            "yzh_public_key" => $this->yzh_public_key,
            "sign_type" => "rsa"
        ));
        try {
            $client = new \Yzh\ApiUserSignServiceClient($config);
            $client->setEnv(\Yzh\ApiUserSignServiceClient::ENV_PROD);
        } catch (\Exception $e) {
            return ['code'=>'500','msg'=>'调用云账户用户签约失败'.$e->getMessage(),'data'=>array()];
        }
        $request = new \Yzh\Model\Apiusersign\ApiUserSignRequest(array(
            "dealer_id" => $this->app_dealer_id,
            "broker_id" => $this->app_broker_id,
            "real_name" => $personal_name,
            "id_card" => $personal_idcard,
            "card_type" => "idcard",
        ));
        $request->setRequestID(time().rand(111,999).rand(11,99));
        try {
            $response = $client->apiUserSign($request);
            if ($response->isSuccess()) {
                // 签约成功
                fwrite(fopen('./uploads/adminyunzhanghuqianyue.txt', 'a+'), "data:".'//'.$member_id.'//'.$personal_name.'@@'.$personal_idcard.'//签约成功'.date("Y-m-d H:i:s")."\r\n");
                Db::table('fa_member')->where('member_id', $member_id)->update(array('me_yzh'=>1));
                return ['code'=>'200','msg'=>'云账户签约成功','data'=>array()];
            } else {
                // 账户已经签约
                fwrite(fopen('./uploads/adminyunzhanghuqianyue.txt', 'a+'), "重复签约data:".'//'.$member_id.'//'.$personal_name.'@@'.$personal_idcard.'//重复签约'.date("Y-m-d H:i:s")."\r\n");
                Db::table('fa_member')->where('member_id', $member_id)->update(array('me_yzh'=>1));
                return ['code'=>'200','msg'=>'用户已经签约云账户','data'=>array()];
            }
        } catch(Exception $e) {
            // 发生异常
            return ['code'=>'500','msg'=>'云账户签约异常：'.$e->getMessage(),'data'=>array()];
        }
    }


    /**
     * 身份证实名验证
     * User:@hang
     */
    public function getNameIdcardIs($name,$idcard){
        // 配置基础信息
        $config = \Yzh\Config::newFromArray(array(
            "app_dealer_id" => $this->app_dealer_id,
            "app_broker_id" => $this->app_broker_id,
            "app_key" => $this->app_key,
            "app_des3_key" => $this->app_des3_key,
            "app_private_key" => $this->app_private_key,
            "yzh_public_key" => $this->yzh_public_key,
            "sign_type" => "rsa"
        ));
        $client = new \Yzh\AuthenticationClient($config);
        $client->setEnv(\Yzh\AuthenticationClient::ENV_PROD);// 沙箱环境使用 ENV_SANDBOX
        // 配置请求参数
        $request = new \Yzh\Model\Authentication\IDCardVerifyRequest(array(
            "real_name" => $name,
            "id_card" => $idcard,
        ));
        // request-id：请求ID，请求的唯一标识
        // 建议平台企业自定义 request-id，并记录在日志中，便于问题发现及排查
        // 如未自定义 request-id，将使用 SDK 中的 random 方法自动生成。注意：random 方法生成的 request-id 不能保证全局唯一，推荐自定义 request-id
        $request->setRequestID(time().rand(1001,9999).rand(11,99));
        try {
            $response = $client->iDCardVerify($request);
            if ($response->isSuccess()) {
                // 操作成功
                return ['code'=>'200','msg'=>'验证成功','data'=>array()];
            } else {
                // 失败返回
                return ['code'=>'500','msg'=>'实名认证失败：'.$response->getMessage(),'data'=>array()];
//                return ['code'=>'500','msg'=>'云账户验证实名认证：'."失败返回 code：" . $response->getCode() . " message：" . $response->getMessage() . " request-id：" . $response->getRequestID(),'data'=>array()];
            }
        } catch(Exception $e) {
            // 发生异常
            return ['code'=>'500','msg'=>'云账户验证实名认证：'.$e->getMessage(),'data'=>array()];
        }
    }



    
    
}
